/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 战斗
	/// module : battle
	/// </summary>
	public interface CNetBattle : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction localNotifyCreateBattle_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<LocalNotifyCreateBattleData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.battle_localNotifyCreateBattle, "Recv->" + _wrapper.message.ToString());
#endif
                SC_localNotifyCreateBattle(session, _wrapper.message.playerPOD, _wrapper.message.battlePOD);
                MessageWrapperPool<LocalNotifyCreateBattleData>.Put(_wrapper);
            };
            MessageFunction localNotifyShutdownBattle_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<LocalNotifyShutdownBattleData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.battle_localNotifyShutdownBattle, "Recv->" + _wrapper.message.ToString());
#endif
                SC_localNotifyShutdownBattle(session, _wrapper.message.battleId);
                MessageWrapperPool<LocalNotifyShutdownBattleData>.Put(_wrapper);
            };
            MessageFunction notifyBattleSettlement_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<NotifyBattleSettlementData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.battle_notifyBattleSettlement, "Recv->" + _wrapper.message.ToString());
#endif
                SC_notifyBattleSettlement(session, _wrapper.message.battleSettlementPOD);
                MessageWrapperPool<NotifyBattleSettlementData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.battle_localNotifyCreateBattle, (session, package) => {
                	var result = MessageWrapperPool<LocalNotifyCreateBattleData>.Get();
                	result.Init(session, localNotifyCreateBattle_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.battle_localNotifyShutdownBattle, (session, package) => {
                	var result = MessageWrapperPool<LocalNotifyShutdownBattleData>.Get();
                	result.Init(session, localNotifyShutdownBattle_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.battle_notifyBattleSettlement, (session, package) => {
                	var result = MessageWrapperPool<NotifyBattleSettlementData>.Get();
                	result.Init(session, notifyBattleSettlement_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        ///  通知创建本地战斗，并直接进入
        /// </summary>
		protected void SC_localNotifyCreateBattle(ISession session, LogicPlayerPOD playerPOD, CreateBattlePOD battlePOD);
		/// <summary>
        ///  通知退出本地战斗(强制踢出)
        /// </summary>
		protected void SC_localNotifyShutdownBattle(ISession session, long battleId);
		/// <summary>
        ///  通知战斗结算
        /// </summary>
		protected void SC_notifyBattleSettlement(ISession session, BattleSettlementPOD battleSettlementPOD);
#endregion
#region Server-side Interface
		/// <summary>
        ///  通知战斗开始
        /// </summary>
    	public bool CS_localNotifyBattleStart(long battleId) 
    	{
    		return _CS_localNotifyBattleStart(battleId);
    	}
    	public bool _CS_localNotifyBattleStart(long battleId)
        {
        	ISession session = GetSession();
        	if(session == null) return false;
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.battle_localNotifyBattleStart, "Send->"+"Battle.CS_localNotifyBattleStart { "
				+ "battleId="
				+ battleId
				+" }");
#endif
        	return session.SendPack(Pack_for_localNotifyBattleStart(battleId));
        }
		
		public static SendPackage Pack_for_localNotifyBattleStart(long battleId) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.battle_localNotifyBattleStart;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(battleId);
			return si_p;
		}
		/// <summary>
        ///  通知战斗结果
        /// </summary>
    	public bool CS_localNotifyBattleEnd(NotifyBattleEndPOD battleEndPOD) 
    	{
    		return _CS_localNotifyBattleEnd(battleEndPOD);
    	}
    	public bool _CS_localNotifyBattleEnd(NotifyBattleEndPOD battleEndPOD)
        {
        	ISession session = GetSession();
        	if(session == null) return false;
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.battle_localNotifyBattleEnd, "Send->"+"Battle.CS_localNotifyBattleEnd { "
				+ "battleEndPOD="
				+ battleEndPOD
				+" }");
#endif
        	return session.SendPack(Pack_for_localNotifyBattleEnd(battleEndPOD));
        }
		
		public static SendPackage Pack_for_localNotifyBattleEnd(NotifyBattleEndPOD battleEndPOD) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.battle_localNotifyBattleEnd;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(battleEndPOD);
			return si_p;
		}
		/// <summary>
        ///  通知退出战斗
        /// </summary>
    	public bool CS_localNotifyBattleExit(long battleId) 
    	{
    		return _CS_localNotifyBattleExit(battleId);
    	}
    	public bool _CS_localNotifyBattleExit(long battleId)
        {
        	ISession session = GetSession();
        	if(session == null) return false;
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.battle_localNotifyBattleExit, "Send->"+"Battle.CS_localNotifyBattleExit { "
				+ "battleId="
				+ battleId
				+" }");
#endif
        	return session.SendPack(Pack_for_localNotifyBattleExit(battleId));
        }
		
		public static SendPackage Pack_for_localNotifyBattleExit(long battleId) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.battle_localNotifyBattleExit;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(battleId);
			return si_p;
		}
#endregion
#region Messages
        public struct LocalNotifyCreateBattleData : IReceiveMessage {
	        public LogicPlayerPOD playerPOD;
	        public CreateBattlePOD battlePOD;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                playerPOD = _in.HasRemaining() ? _in.Read_pod<LogicPlayerPOD>(LogicPlayerPOD.DynamicFactory) : default;
                battlePOD = _in.HasRemaining() ? _in.Read_pod<CreateBattlePOD>(CreateBattlePOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.battle_localNotifyCreateBattle;}
            
            public override string ToString()
            {
                return "Battle.localNotifyCreateBattle"+ ", playerPOD="+ playerPOD+ ", battlePOD="+ battlePOD;
            }
        }
        public struct LocalNotifyShutdownBattleData : IReceiveMessage {
	        public long battleId;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                battleId = _in.HasRemaining() ? _in.Read_long() : default;
            }
            
            public int GetMessageId() {return NetMessageId.battle_localNotifyShutdownBattle;}
            
            public override string ToString()
            {
                return "Battle.localNotifyShutdownBattle"+ ", battleId="+ battleId;
            }
        }
        public struct NotifyBattleSettlementData : IReceiveMessage {
	        public BattleSettlementPOD battleSettlementPOD;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                battleSettlementPOD = _in.HasRemaining() ? _in.Read_pod<BattleSettlementPOD>(BattleSettlementPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.battle_notifyBattleSettlement;}
            
            public override string ToString()
            {
                return "Battle.notifyBattleSettlement"+ ", battleSettlementPOD="+ battleSettlementPOD;
            }
        }
    }
#endregion
}